//
//  DKVisitorView.swift
//  Sina
//
//  Created by  duan on 16/4/7.
//  Copyright © 2016年 duan. All rights reserved.
//  访客模式的自定义视图:




import UIKit

class DKVisitorView: UIView {

    //向外抛出一个方法,用于自定义设置注册视图:
    func setVisitorView(backImage: String?, labelText: String){
    messageLabel.text = labelText
       //守卫一下,如果是家界面就不再往下进行
        guard let backImage = backImage else{
            print("这里是家界面")
            //执行动画:
            animations()
        return
        }
        
    backImageView.image = UIImage(named: backImage)
        //设置遮盖隐藏:
        homeImageView.hidden = true
     //将遮盖界面带到最下面:
        sendSubviewToBack(coverImageView)
    }
    
    
    //滚轮转动的方法:
    
    private func animations() {
        
        //创建Basic动画:
        let an = CABasicAnimation(keyPath: "transform.rotation")
        
        
        an.toValue = M_PI * 2
        an.duration = 30
        an.repeatCount = MAXFLOAT
        
        //设置视图不销毁时动画不移除:
        an.removedOnCompletion = false
        
        
        //将动画加到layer层上:
        backImageView.layer.addAnimation(an, forKey: nil)
        
        
    }
    
    //重写父类的这个方法后还要重写下面方法,用于阻止sb 或xib 使用这个类
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //自定义视图:
        setUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        //如果注释掉这个方法,sb或xib就可以使用这个类
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUI()
    }
    
    //MARK: - 懒加载子视图
    //转轮视图:
    lazy private var backImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
   
    //遮罩背景:
    lazy private var coverImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子视图:
    lazy private var homeImageView: UIImageView  = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 消息文字
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人,回到这里看看有什么惊喜.关注一些人,回到这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGrayColor())
    //注册按钮:
    private lazy var registBt: UIButton = UIButton(text: "注册", textSize: 16, color: UIColor.orangeColor(), backGroundImage: "common_button_white_disable")
    //登录按钮:
    private lazy var loginBt: UIButton = UIButton(text: "登录", textSize: 16, color: UIColor.darkGrayColor(), backGroundImage: "common_button_white_disable")
    
    
    
    
}

extension DKVisitorView {
    
    private func setUI(){
        //
        addSubview(backImageView)
        addSubview(coverImageView)
        addSubview(homeImageView)
        addSubview(messageLabel)
        addSubview(registBt)
        addSubview(loginBt)
        
//        //自定义视图需要更改这个属性为false 才能正确显示位置
//  backImageView.translatesAutoresizingMaskIntoConstraints = false
//    homeImageView.translatesAutoresizingMaskIntoConstraints = false
        
        for v in subviews{
            
      v.translatesAutoresizingMaskIntoConstraints = false
            
        }
        
        
        //设置布局:
        ///设置转盘的布局
        addConstraint(NSLayoutConstraint(item: backImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: backImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        
        
        
        
        
        ///设置小房子的布局:
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterX, relatedBy: .Equal, toItem: backImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImageView, attribute: .CenterY, relatedBy: .Equal, toItem: backImageView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置label的布局:
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: backImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterY, relatedBy: .Equal, toItem: backImageView, attribute: .BottomMargin, multiplier: 1.0, constant: 30))
        //设置lable的宽高约束:
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 240))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
            //布局注册按钮:
        addConstraint(NSLayoutConstraint(item: registBt, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBt, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //宽100,高35
        addConstraint(NSLayoutConstraint(item: registBt, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBt, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        //登录注册按钮:
        addConstraint(NSLayoutConstraint(item: loginBt, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBt, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        //宽100,高35
        addConstraint(NSLayoutConstraint(item: loginBt, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBt, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        
        ///遮罩的布局:
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverImageView]-0-|", options: [], metrics: nil, views: ["coverImageView" : coverImageView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-(margin)-[coverImageView]-(marginH)-[backImageView]", options: [], metrics: ["margin" : backImageView.bounds.height*0.5, "marginH":-backImageView.bounds.height], views: ["coverImageView" : coverImageView, "backImageView" : backImageView]))
        
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        
        
        
        
        
}
}